import _ from "lodash";

const formatPower = bytes => {
	if (!bytes) return { n: 0, unit: '' };
	if (_.isString(bytes)) {
		bytes = _.toNumber(bytes);
	}
	if (!bytes || bytes == 0) return { n: 0, unit: '' };
	let k = 1024;
	let sizeStr = ["", "K", "M", "G", "T", "P"];
	let i = 0;
	for (let l = 0; l < 8; l++) {
		if (bytes / Math.pow(k, l) < 1) {
			break;
		}
		i = l;
	}
	return { n: parseFloat((bytes / Math.pow(k, i)).toFixed(2)), unit: sizeStr[i] };
};
const formatPowerStr = bytes => {
	let { n, unit } = formatPower(bytes);
	return `${n.toLocaleString("zh", { style: "decimal" })} ${unit}`;
}
/**
 * formatter file size
 * @param bytes
 */
const formatSize = bytes => {
	if (!bytes) return { n: 0, unit: 'KB' };
	if (_.isString(bytes)) {
		bytes = _.toNumber(bytes);
	}
	if (!bytes || bytes == 0) return { n: 0, unit: 'KB' };
	let k = 1024;
	let sizeStr = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; //store
	let i = 0;
	for (let l = 0; l < 8; l++) {
		if (bytes / Math.pow(k, l) < 1) {
			break;
		}
		i = l;
	}
	return { n: parseFloat((bytes / Math.pow(k, i)).toFixed(2)), unit: sizeStr[i] };
};
const formatterSize = bytes => {
	let { n, unit } = formatSize(bytes);
	return `${n} ${unit}`;
};

/**
 * formatter file size from mb
 * @param bytes
 */
const formatterSizeFromMB = bytes => {
	if (_.isString(bytes)) {
		bytes = _.toNumber(bytes);
	}
	if (bytes == 0) return "0 MB";
	let k = 1024; //set base size
	let sizeStr = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; //size
	let i = 0; //Unit subscript and power
	for (let l = 0; l < 6; l++) {
		if (bytes / Math.pow(k, l) < 1) {
			break;
		}
		i = l;
	}
	return (bytes / Math.pow(k, i)).toFixed(3) + " " + sizeStr[i];
};
const formatterSizeFromMBToGB = bytes => {
	if (_.isString(bytes)) {
		bytes = _.toNumber(bytes);
	}
	let g = 1024 * 1024 * 1024;
	return (bytes / g).toFixed(3);
};
const formatterSizeToTB = bytes => {
	if (_.isString(bytes)) {
		bytes = _.toNumber(bytes);
	}
	let g = 1024 * 1024 * 1024 * 1024;
	return parseFloat((bytes / g).toFixed(3)).toLocaleString("zh", { style: "decimal" });
};
/**
 * formatter coin
 * @param coin
 */
const formatterCurrency = coin => {
	if (!coin || isNaN(coin)) return { n: 0, unit: '' };
	if (typeof coin != 'number') {
		coin = parseFloat(coin);
	}
	coin = coin / 1e18;
	let unit = '';
	if (coin > 1000) {
		coin = coin / 1000;
		unit = ' K';
		if (coin > 1000) {
			coin = coin / 1000;
			unit = ' M';
			if (coin > 1000) {
				coin = coin / 1000;
				unit = ' B';
			}
		}
	}
	coin = Math.round(coin * 100000) / 100000;
	coin = coin.toLocaleString("zh", { style: "decimal" });
	return { n: coin, unit };
};
/**
 * formatter coin
 * @param coin
 */
const formatterCurrencyStr = coin => {
	let { n, unit } = formatterCurrency(coin);
	return n + ' ' + unit;
};
const formatterCurrencyStr2 = coin => {
	if (_.isString(coin)) {
		coin = _.toNumber(coin);
	}
	if (coin == 0) return 0;
	coin = coin / 1e18;
	coin = Math.round(coin * 10000) / 10000;
	coin = coin.toLocaleString("zh", { style: "decimal", maximumFractionDigits: 4 });
	return coin;
};
const formatterCurrencyStr3 = coin => {
	if (_.isString(coin)) {
		coin = _.toNumber(coin);
	}
	if (coin == 0) return 0;
	coin = coin / 1e18;
	coin = coin.toLocaleString("zh", { style: "decimal", maximumFractionDigits: 18 });
	return coin;
};
const toLocaleString = coin => {
	if (!coin) return coin;
	if (_.isString(coin)) {
		coin = _.toNumber(coin);
	}
	if (coin == 0) return 0;
	coin = Math.round(coin * 100) / 100;
	coin = coin.toLocaleString("zh", { style: "decimal" });
	return coin;
};
const toLocaleStringWithUnit = coin => {
	if (!coin || isNaN(coin)) return "0";
	if (typeof coin != 'number') {
		coin = parseFloat(coin);
	}
	let unit = '';
	if (coin > 1000) {
		coin = coin / 1000;
		unit = ' K';
		if (coin > 1000) {
			coin = coin / 1000;
			unit = ' M';
			if (coin > 1000) {
				coin = coin / 1000;
				unit = ' B';
			}
		}
	}
	coin = Math.round(coin * 100000) / 100000;
	coin = coin.toLocaleString("zh", { style: "decimal" });
	return coin + unit;
};

/**
 * check is json
 * @param str
 */


function toFixed(v) {
	return Math.floor(parseFloat(v) * 100) / 100;
}

function formatAddress(addr) {
	if (!addr || typeof addr != 'string') return "";
	if (addr.length < 10) return addr;
	return addr.slice(0, 5) + "..." + addr.slice(-5);
}
function formatAddressLong(addr) {
	if (!addr) return "";
	if (addr.length < 26) return addr;
	return addr.slice(0, 13) + "..." + addr.slice(-13);
}

export {
	formatPower,
	formatPowerStr,
	formatterSize,
	toFixed,
	formatSize,
	formatterSizeFromMB,
	formatterSizeFromMBToGB,
	formatterSizeToTB,
	formatterCurrency,
	formatterCurrencyStr,
	formatterCurrencyStr2,
	formatterCurrencyStr3,
	toLocaleString,
	toLocaleStringWithUnit,
	formatAddress,
	formatAddressLong
};
